package com.jsp.controller.usercontroller;

import com.alibaba.fastjson.JSON;
import com.jsp.entity.UserVO;
import com.jsp.entity.Bzdm;
import com.jsp.entity.Depart;
import com.jsp.entity.User;
import com.jsp.mapper.impl.BzdmMapperImpl;
import com.jsp.mapper.impl.DepartMapperImpl;
import com.jsp.mapper.impl.UserMapperImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.jsp.controller.usercontroller.UserLoginServlet.getUserVOS;

@WebServlet(name = "UserListServlet", value = "/userList")
public class UserListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {

        try
        {
            List<UserVO> collect = getUserVOS();
            String bmmc = request.getParameter("bmmc");
            if (null != bmmc && !"".equals(bmmc)) {
                collect = collect.stream().filter(item -> bmmc.equals(item.getDepart())).collect(Collectors.toList());
            }
            String username = request.getParameter("username");
            if (null != username && !"".equals(username)) {
                collect = collect.stream().filter(item -> username.equals(item.getYhid()) || username.equals(item.getYhxm())).collect(Collectors.toList());
            }
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");


            String s = JSON.toJSONString(collect);
            response.getWriter().print(s);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
//        try {
//            String username = request.getParameter("username");
//            String bmmc = request.getParameter("BMMC");
//            BzdmMapperImpl bzdmMapper = new BzdmMapperImpl();
//            Map<String, Bzdm> sexs = bzdmMapper.findAll().stream()
//                    .collect(Collectors.toMap(k -> k.getCode(), v -> v));
//            DepartMapperImpl departMapper = new DepartMapperImpl();
//            Map<String, Depart> departs = departMapper.findAll().stream()
//                    .collect(Collectors.toMap(k -> k.getBmdm(), v -> v));
//
//            UserMapperImpl userMapper = new UserMapperImpl();
//            List<User> all = userMapper.findAll();
//            List<UserVO> collect = all.stream().map(user -> {
//                UserVO userVO = new UserVO();
//                userVO.setYhxm(user.getYhxm());
//                userVO.setYhid(user.getYhid());
//                userVO.setSex(sexs.get(user.getYhxb()).getMc());
//                userVO.setDepart(departs.get(user.getYhbm()).getBmmc());
//                return userVO;
//            }).collect(Collectors.toList());
//
//            // 条件筛选
//            if (null != bmmc && !"".equals(bmmc)) {
//                collect = collect.stream().filter(userVO -> bmmc.equals(userVO.getDepart())).collect(Collectors.toList());
//                request.setAttribute("userList", collect);
//                request.setAttribute("departList", departMapper.findAll());
//                request.getRequestDispatcher("/list.jsp").forward(request, response);
//                return;
//            }
//
//
//            if (null != username && !"".equals(username)) {
//                collect = collect.stream().filter(userVO -> username.equals(userVO.getYhid()) || username.equals(userVO.getYhkl())).collect(Collectors.toList());
//
//            }
//            request.setAttribute("userList", collect);
//            request.setAttribute("departList", departMapper.findAll());
//            request.getRequestDispatcher("/list.jsp").forward(request, response);
//        }catch (ServletException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


    }
}
